package it.unical.mat.aec.gui;

import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.util.HashMap;

import it.unical.mat.aec.model.Traccia;
import it.unical.mat.aec.persistence.PersistanceException;
import it.unical.mat.aec.processTests.ProcessTestsFacade;

import javax.swing.DefaultListModel;
import javax.swing.JButton;
import javax.swing.JList;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.ListModel;
import javax.swing.ListSelectionModel;
import javax.swing.SwingUtilities;

public class VisualizzaTraccePanel extends JPanel {

	private JList listaTracce;
	private HashMap<Integer, Traccia> tracce;
	private int selected;
	private JPanel viewPanel;
	private JButton okButton;
	private JButton newTrackButton;
	private ProcessTestsFacade processTestsFacade;
	private JPanel buttonPanel;
	private VisualizzaTracciaPanel tracciaPanel;
	
	public VisualizzaTraccePanel() {
		super(new BorderLayout());
		try {
			processTestsFacade = ProcessTestsFacade.getInstance();
			tracce = new HashMap<Integer, Traccia>();
			tracce = processTestsFacade.getTracce();
			visualizzaId(tracce);
			viewPanel = new JPanel(new BorderLayout());
			buttonPanel=new JPanel();
			tracciaPanel=new VisualizzaTracciaPanel(tracce.get(selected));
			viewPanel.add(tracciaPanel,BorderLayout.CENTER);
			add(listaTracce, BorderLayout.WEST);
			add(viewPanel, BorderLayout.CENTER);
			viewPanel.add(buttonPanel,BorderLayout.SOUTH);
			okButton = new JButton("Assegna Traccia");
			buttonPanel.add(okButton, BorderLayout.SOUTH);
			okButton.addActionListener(new ActionListener() {

				@Override
				public void actionPerformed(ActionEvent arg0) {
					try {
						processTestsFacade.associaTraccia(selected);
						JOptionPane.showMessageDialog(VisualizzaTraccePanel.this, "Traccia Associata: "+selected);
						MainFrame.getInstance().correzionePanel();
					} catch (PersistanceException e) {
						JOptionPane.showMessageDialog(
								VisualizzaTraccePanel.this, e.getMessage(),
								"Error", JOptionPane.ERROR_MESSAGE);
					}

				}
			});
		} catch (PersistanceException e) {
			JOptionPane.showMessageDialog(this, e.getMessage(), "Error",
					JOptionPane.ERROR_MESSAGE);
		}
	}

	private void visualizzaId(HashMap<Integer, Traccia> tracce) {
		String [] domande=new String[tracce.keySet().size()];
		int j=0;
		listaTracce = new JList(new DefaultListModel());
		listaTracce.setSelectedIndex(ListSelectionModel.SINGLE_SELECTION);
		listaTracce.addMouseListener(new MouseHandlerSelect());
		listaTracce.setFocusable(true);
		listaTracce.setPreferredSize(new Dimension(300, 600));
		for(Integer i:tracce.keySet()){
			domande[j]="Traccia id:"+i;
			j++;
		}
		listaTracce.setListData(domande);

	}

	private final class MouseHandlerSelect extends MouseAdapter {
		@Override
		public void mousePressed(MouseEvent e) {
			String []select=((String) listaTracce.getSelectedValue()).split(":");
			selected = Integer.valueOf(select[1]);
			switchTO(new VisualizzaTracciaPanel(tracce.get(selected)));
		}
	}
	
	protected void switchTO(final JPanel panel) {
		SwingUtilities.invokeLater(new Runnable() {
			@Override
			public void run() {
				tracciaPanel.removeAll();
				tracciaPanel.add(panel, BorderLayout.CENTER);
				tracciaPanel.updateUI();
				panel.requestFocus();
			}
		});
	}
}